#ifndef __UDP_SERVER_HPP__

#define __UDP_SERVER_HPP__

#include <iostream>
#include <string>
#include <memory>
#include <cstring>
#include <cerrno>
#include <strings.h>
#include <functional>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "InetAddr.hpp"
#include "Log.hpp"
#include "Common.hpp"
#include "ThreadPool.hpp"

using namespace LogModule;
using namespace ThreadPoolModule;

const static int gsockfd = -1;
// const static std::string gdefaultip = "127.0.0.1"; // 表示本地主机，本地通信用该ip绑定server
const static uint16_t gdefaultport = 8080; // 自行指定非特定的port，

// void AddrUser(const InetAddr &id)
using adduser_t = std::function<void(InetAddr &id)>;

// void Router(int sockfd, const std::string &message)
using route_t = std::function<void(int sockfd, const std::string &message)>; // TODO??

using task_t = std::function<void()>; // 交付线程池的任务类型

class UdpServer
{
public:
    UdpServer(uint16_t port = gdefaultport)
        : _sockfd(gsockfd),
          _addr(port), // call InetAddr(uint16_t port)
          _isrunning(false)
    {
    }

    void InitServer()
    { // 1.创建socket
        _sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(LogLevel::FATAL) << "socket: " << strerror(errno);
            Die(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket success, socket fd is : " << _sockfd;
        // 2.2 bind ： 设置进入内核之中
        int n = ::bind(_sockfd, _addr.NetAddr(), _addr.NetAddrLen());
        if (n < 0)
        {
            LOG(LogLevel::FATAL) << "bind: " << strerror(errno);
            Die(BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success";
    }
    // 注册方法：adduser_t和route_t
    void RegisterService(adduser_t adduser, route_t route)
    {
        _adduser = adduser;
        _route = route;
    }
    void Start()
    {
        _isrunning = true;
        while (true)
        {
            char inbuffer[1024];
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer); // 必须设定

            ssize_t n = ::recvfrom(_sockfd, inbuffer, sizeof(inbuffer) - 1, 0, CONV(&peer), &len);
            if (n > 0)
            {

                InetAddr cli(peer);
                inbuffer[n] = 0;

                std::string message =cli.Addr() + "#" + inbuffer;

                

                // 2.新增用户 -- 服务器发现用户后新增用户
                _adduser(cli);

                // 3.构建转发任务，传递给线程池，让线程池进行转发
                // TODO???
                //_route的类型是route_t，而task_t可以接收route_t类型?             //TODO?
                task_t task = std::bind(UdpServer::_route, _sockfd, message); // TODO??
                ThreadPool<task_t>::getInstance()->Equeue(task);
            }
        }
        _isrunning = false;
    }
    ~UdpServer()
    {
        if (_sockfd > gsockfd)
            ::close(_sockfd);
    }

private:
    int _sockfd;
    InetAddr _addr;
    bool _isrunning;

    adduser_t _adduser; // 新增用户
    route_t _route;     // 数据转发
};

#endif